#include <iostream>
#include <vector>

using namespace std;

bool check[51][51];
int m = 0, n = 0;

void dfs(vector<vector<char>>& board, int r = 0, int c = 0)
{
    check[r][c] = true;
    if (board[r][c] == 'M')
    {
        board[r][c] = 'X';
        return;
    }
    if (board[r][c] == 'E')
    {
        int count = 0;
        if (r - 1 >= 0 && c - 1 >= 0 && board[r - 1][c - 1] == 'M')
            ++count;
        if (r - 1 >= 0 && board[r - 1][c] == 'M')
            ++count;
        if (r - 1 >= 0 && c + 1 < n && board[r - 1][c + 1] == 'M')
            ++count;
        if (c - 1 >= 0 && board[r][c - 1] == 'M')
            ++count;
        if (c + 1 < n && board[r][c + 1] == 'M')
            ++count;
        if (r + 1 < m && c - 1 >= 0 && board[r + 1][c - 1] == 'M')
            ++count;
        if (r + 1 < m && board[r + 1][c] == 'M')
            ++count;
        if (r + 1 < m && c + 1 < n && board[r + 1][c + 1] == 'M')
            ++count;
        if (count > 0)
        {
            board[r][c] = '0' + count;
        }
        else
        {
            board[r][c] = 'B';
            if (r - 1 >= 0 && c - 1 >= 0 && !check[r - 1][c - 1])
                dfs(board, r - 1, c - 1);
            if (r - 1 >= 0 && !check[r - 1][c])
                dfs(board, r - 1, c);
            if (r - 1 >= 0 && c + 1 < n && !check[r - 1][c + 1])
                dfs(board, r - 1, c + 1);
            if (c - 1 >= 0 && !check[r][c - 1])
                dfs(board, r, c - 1);
            if (c + 1 < n && !check[r][c + 1])
                dfs(board, r, c + 1);
            if (r + 1 < m && c - 1 >= 0 && !check[r + 1][c - 1])
                dfs(board, r + 1, c - 1);
            if (r + 1 < m && !check[r - 1][c])
                dfs(board, r + 1, c);
            if (r + 1 < m && c + 1 < n && !check[r + 1][c + 1])
                dfs(board, r + 1, c + 1);
        }
    }
}

vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click)
{
    m = board.size();
    n = board[0].size();
    dfs(board, click[0], click[1]);
    return board;
}

int main()
{
    vector<vector<char>> board = { {'E', 'E', 'E', 'E', 'E'}, {'E', 'E', 'M', 'E', 'E'}, {'E', 'E', 'E', 'E', 'E'}, {'E', 'E', 'E', 'E', 'E'}, {'E', 'E', 'E', 'E', 'E'} };
    vector<int> click = { 3, 0 };
    updateBoard(board, click);
    return 0;
}